﻿#include <QPainter>
#include <QMouseEvent>
#include <qmath.h>
#include "CMapWidgetBase.h"
#include "CHeader.h"
CMapWidgetBase::CMapWidgetBase(QWidget * parent)
	: QWidget(parent), m_isDragging(false), m_scale(1.0), m_translation(0, 0)
{
	setMouseTracking(true);//打开鼠标实时追踪功能
	m_cWidgetSize = QWidget::size();
	m_bChargeFlicker = true;
	m_bFlicker = true;
	m_nXMax = 100000;
	m_nXMin = 0;
	m_nYMax = 10000;
	m_nYMin = 0;
	m_nXval = 0;
	m_dRotateAngle = 0.0;
	m_dRotateAngleOld = 0.0;
}

CMapWidgetBase::~CMapWidgetBase()
{
}

void CMapWidgetBase::setPoints(const QVector<QPointF>& points, QVector<int> vecXData)
{
	m_vecOripoints = points;
	m_vecXdata = vecXData;
	GetXYMaxMin();//获取最大值与最小值
	update();
}


void CMapWidgetBase::setActPoints(int nVal, bool bCharge)
{
	m_nXval = nVal;
	GetDrawActPoint();
	m_bFlicker = !m_bFlicker;
	m_bChargeFlicker = bCharge;
	update();
}



void CMapWidgetBase::resizeEvent(QResizeEvent * event)
{
	m_cWidgetSize = QWidget::size();
	GetDrawActPoint();
	update();
	//GetDrawPoint();
}

void CMapWidgetBase::paintEvent(QPaintEvent * event)
{
	if (m_vecPoints.size() < 1 || m_vecActPoints.size() < 1)
	{
		return;
	}

	QPainter painter(this);                        //创建了一个 QPainter 对象，并将当前窗口或部件（this）作为绘制设备。
	painter.setRenderHint(QPainter::Antialiasing); //设置了抗锯齿渲染,可以改善绘制的图形的平滑度和质量。
	painter.translate(0, 0);                        //将坐标系统进行平移操作，将坐标系原点移动到 (0, 0) 点。
	painter.scale(m_scale, m_scale);               //进行坐标系统的缩放操作，通过缩放因子 m_scale 对坐标系进行缩放。
	painter.translate(m_translation);              //再次进行平移操作，根据 m_translation 的值移动坐标系的原点。

	// 画线段
	painter.setPen(QPen(QColor(82, 91, 105), DiamSmall / 2));
	for (int i = 0; i < m_vecPoints.size() - 1; i++)
	{
		painter.drawLine(m_vecPoints[i], m_vecPoints[i + 1]);
	}

	//画圆
	painter.setPen(Qt::NoPen);//不绘制图形的边框线
	painter.setBrush(QBrush(QColor(147, 147, 147)));
	for (const QPointF &point : m_vecPoints)
	{
		QRect rect1 = QRect(point.x() - DiamSmall / 2, point.y() - DiamSmall / 2, DiamSmall, DiamSmall);
		painter.drawEllipse(rect1);//画椭圆
	}

	//画线段
	painter.setPen(Qt::NoPen);
	painter.setPen(QPen(QColor(0, 205, 255), DiamSmall / 2));
	if (m_vecActPoints.size() > 1)
	{
		for (int i = 0; i < m_vecActPoints.size() - 1; i++)
		{
			painter.drawLine(m_vecActPoints[i], m_vecActPoints[i + 1]);
		}
	}
	//画圆
	painter.setPen(Qt::NoPen);
	painter.setBrush(QBrush(QColor(255, 255, 255)));
	for (int i = 0; i < m_vecActPoints.size(); i++)
	{
		QRect rect1 = QRect(m_vecActPoints[i].x() - DiamLarge / 2, m_vecActPoints[i].y() - DiamLarge / 2, DiamLarge, DiamLarge);
		painter.drawEllipse(rect1);
	}
	painter.setPen(Qt::NoPen);
	painter.setBrush(QBrush(QColor(53, 215, 255)));
	for (int i = 0; i < m_vecActPoints.size(); i++)
	{
		QRect rect1 = QRect(m_vecActPoints[i].x() - DiamSmall / 2, m_vecActPoints[i].y() - DiamSmall / 2, DiamSmall, DiamSmall);
		painter.drawEllipse(rect1);
	}
	QPointF cPoint = m_vecActPoints[m_vecActPoints.size() - 1];
	//画光环
	if (m_bFlicker)
	{
		QRect rect3 = QRect(cPoint.x() - DiamGuanghuanLarge / 2, cPoint.y() - DiamGuanghuanLarge / 2, DiamGuanghuanLarge, DiamGuanghuanLarge);
		painter.setPen(Qt::NoPen);
		painter.setBrush(QBrush(QColor(9, 85, 118, 120)));
		painter.drawEllipse(rect3);

		QRect rect2 = QRect(cPoint.x() - DiamGuanghuanSmall / 2, cPoint.y() - DiamGuanghuanSmall / 2, DiamGuanghuanSmall, DiamGuanghuanSmall);
		painter.setPen(Qt::NoPen);
		painter.setBrush(QBrush(QColor(102, 149, 168, 120)));
		painter.drawEllipse(rect2);

		QRect rect4 = QRect(cPoint.x() - DiamGuanghuan / 2, cPoint.y() - DiamGuanghuan / 2, DiamGuanghuan, DiamGuanghuan);
		painter.setPen(Qt::NoPen);
		painter.setBrush(QBrush(QColor(12, 151, 193, 180)));
		painter.drawEllipse(rect4);

		QRect rect = QRect(cPoint.x() - DiamLarge / 2, cPoint.y() - DiamLarge / 2, DiamLarge, DiamLarge);
		painter.setPen(Qt::NoPen);
		painter.setBrush(QBrush(QColor(255, 255, 255)));
		painter.drawEllipse(rect);

		QRect rect1 = QRect(cPoint.x() - DiamSmall / 2, cPoint.y() - DiamSmall / 2, DiamSmall, DiamSmall);
		painter.setPen(Qt::NoPen);
		painter.setBrush(QBrush(QColor(53, 215, 255)));
		painter.drawEllipse(rect1);

		QRect rectJqr = QRect(cPoint.x() - 18, cPoint.y() - 35, 36, 36);
		painter.setBrush(QBrush(QColor(0, 0, 0, 0)));
		painter.drawRect(rectJqr);
		painter.drawPixmap(rectJqr, QPixmap(":/icon/Rob/Rob.ico"));
	}

	int nX = m_cWidgetSize.width();
	int nY = m_cWidgetSize.height();
	//画充电位置
	//QRect rectJqrCd = QRect(nX*0.9,nY*0.1,nX*0.1, nY*0.1);
	QRect rectJqrCd = QRect(nX*0.1*0.9, nY*0.1*0.9 + 18, 20, 10);
	painter.setPen(Qt::NoPen);
	painter.setBrush(QBrush(QColor(0, 0, 0, 0)));
	painter.drawRect(rectJqrCd);
	painter.drawPixmap(rectJqrCd, QPixmap(":/icon/Rob/cdw.png"));

	QRect textRect = QRect(nX*0.1 - 20, nY*0.1*0.9 + 32, 40, 10);
	QFont f = QFont("Microsoft YaHei", 8, QFont::Bold);
	painter.setFont(f);
	painter.setPen(QColor("#FFFFFF"));
	if (m_bChargeFlicker)
	{
		painter.drawText(textRect, Qt::AlignCenter, A2T("充电中"));
	}
	else
	{
		painter.drawText(textRect, Qt::AlignCenter, A2T("充电桩"));
	}

	if (m_bChargeFlicker)
	{
		if (m_bFlicker)
		{
			/*QRect rect = QRect(nX*0.90 - DiamLarge / 2,nY*0.15 - DiamLarge / 2, DiamLarge, DiamLarge);
			painter.setPen(Qt::NoPen);
			painter.setBrush(QBrush(QColor(255, 244, 82)));
			painter.drawEllipse(rect);
			QRect rect1 = QRect(nX*0.90 - DiamSmall / 2, nY*0.15 - DiamSmall / 2, DiamSmall, DiamSmall);
			painter.setPen(Qt::NoPen);
			painter.setBrush(QBrush(QColor(0, 205, 255)));
			painter.drawEllipse(rect1);*/
		}
	}



}

/**
* @brief mouseMoveEvent 鼠标按下事件
* @param event 鼠标事件对象
*/
void CMapWidgetBase::mousePressEvent(QMouseEvent * event)
{
	if (event->button() == Qt::LeftButton)
	{
		m_lastPos = event->pos();
		m_isDragging = true;
	}
}

/**
 * @brief mouseMoveEvent 鼠标移动事件
 * @param event 鼠标事件对象
 */
void CMapWidgetBase::mouseMoveEvent(QMouseEvent * event)
{
	if (m_isDragging)
	{
		QPoint delta = event->pos() - m_lastPos;
		m_translation += delta;
		m_lastPos = event->pos();
		update();
	}
}

/**
 * @brief mouseReleaseEvent 鼠标释放事件
 * @param event 鼠标事件对象
 */
void CMapWidgetBase::mouseReleaseEvent(QMouseEvent * event)
{
	if (event->button() == Qt::LeftButton)
	{
		m_isDragging = false;
	}
}

void CMapWidgetBase::wheelEvent(QWheelEvent * event)
{
	qreal scaleFactor = event->angleDelta().y() > 0 ? 1.1 : 0.9;
	m_scale *= scaleFactor;
	update();
}

void CMapWidgetBase::clearPoints()
{
	QVector<QPointF>().swap(m_vecOripoints);
	m_vecOripoints.clear();
	QVector<int>().swap(m_vecXdata);
	m_vecXdata.clear();
}

void CMapWidgetBase::GetXYMaxMin()
{
	for (int i = 0; i < m_vecOripoints.size(); i++)
	{
		if (m_vecOripoints[i].x() >= m_nXMax)
			m_nXMax = m_vecOripoints[i].x();
		if (m_vecOripoints[i].y() >= m_nYMax)
			m_nYMax = m_vecOripoints[i].y();

		if (m_vecOripoints[i].x() <= m_nXMin)
			m_nXMin = m_vecOripoints[i].x();
		if (m_vecOripoints[i].y() <= m_nYMin)
			m_nYMin = m_vecOripoints[i].y();
	}
	if ((m_vecXdata.size() > 0) && (m_vecOripoints.size() > 0))
	{
		int nX = m_vecXdata[m_vecXdata.size() - 1];
		if (m_nXval > nX)
		{
			double radians = qDegreesToRadians((double)m_dRotateAngleOld);// 将角度转换为弧度
			int nDisX = qAbs((m_nXval - nX)* qCos(radians));
			int nDisY = qAbs((m_nXval - nX)* qSin(radians));
			int nLaxtX = m_vecOripoints[m_vecOripoints.size() - 1].x();
			int nLaxtY = m_vecOripoints[m_vecOripoints.size() - 1].y();
			if ((nLaxtX + nDisX) >= m_nXMax)
				m_nXMax = nLaxtX + nDisX;
			if ((nLaxtY + nDisY) >= m_nYMax)
				m_nYMax = nLaxtY + nDisY;

			if (nLaxtX - nLaxtX <= m_nXMin)
				m_nXMin = nLaxtX - nLaxtX;
			if (nLaxtY - nLaxtY <= m_nYMin)
				m_nYMin = nLaxtY - nLaxtY;
		}
	}
}

void CMapWidgetBase::GetDrawActPoint()
{
	if (m_cWidgetSize.width() <= 0 || m_cWidgetSize.height() <= 0)//确保画布的大小是否有效
	{
		return;
	}
	//清空画图数组
	if (m_vecPoints.size() != m_vecOripoints.size())
	{
		m_vecPoints.clear();
		m_vecPoints.shrink_to_fit();
		m_vecPoints.resize(m_vecOripoints.size());
	}
	m_vecActPoints.clear();
	m_vecActPoints.shrink_to_fit();

	if (m_vecXdata.size() > 0)
	{
		if (m_nXval < m_vecXdata[0])
		{
			m_nXval = m_vecXdata[0];//设置当前机器人运动到的点
		}
	}
	//获取已经执行到的点
	for (int i = 0; i < m_vecOripoints.size(); i++)
	{
		if (m_nXval >= m_vecXdata[i])
		{
			m_vecActPoints.push_back(m_vecOripoints[i]);//执行过的坐标点
		}
		else
		{

			if (m_vecXdata.size() > 1)
			{
				double lengthAB = m_vecXdata[i] - m_vecXdata[i - 1];
				double dABVal = (m_nXval - m_vecXdata[i - 1]) / lengthAB;
				// 计算点C在向量AB上的坐标
				qreal x3 = m_vecOripoints[i - 1].x() + (m_vecOripoints[i].x() - m_vecOripoints[i - 1].x())*dABVal;
				qreal y3 = m_vecOripoints[i - 1].y() + (m_vecOripoints[i].y() - m_vecOripoints[i - 1].y())*dABVal;
				m_vecActPoints.push_back(QPointF(x3, y3));
				break;
			}

		}
	}
	//计算画图点坐标
	double dScale = qMax((m_nXMax - m_nXMin) / m_cWidgetSize.width(), (m_nYMax - m_nYMin) / m_cWidgetSize.height());
	for (int i = 0; i < m_vecPoints.size(); i++)
	{
		double dX = (m_vecOripoints[i].x() - m_nXMin) / dScale;
		double dY = (m_vecOripoints[i].y() - m_nYMin) / dScale;
		m_vecPoints[i].setX(dX*0.8 + m_cWidgetSize.width()*0.1);
		m_vecPoints[i].setY(dY*0.8 + m_cWidgetSize.height()*0.1);
	}
	for (int i = 0; i < m_vecActPoints.size(); i++)
	{
		double dX = (m_vecActPoints[i].x() - m_nXMin) / dScale;
		double dY = (m_vecActPoints[i].y() - m_nYMin) / dScale;
		m_vecActPoints[i].setX(dX*0.8 + m_cWidgetSize.width()*0.1);
		m_vecActPoints[i].setY(dY*0.8 + m_cWidgetSize.height()*0.1);
	}
}


